package com.officegameapp.db.implementation.hibernate;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.officegameapp.db.dao.IGameTableDAO;
import com.officegameapp.db.entities.GameTable;
import com.officegameapp.db.entities.Player;

public class GameTableDAO implements IGameTableDAO {

	@Override
	public void persist(GameTable t) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		session.saveOrUpdate(t);
		session.getTransaction().commit();
	}

	@Override
	public void create(GameTable t) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();
	}

	@Override
	public GameTable read(long id) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		GameTable table = (GameTable) session.createCriteria(GameTable.class)
			.add(Restrictions.eq("id", id)).uniqueResult();
		session.getTransaction().commit();
		return table;
	}

	@Override
	public void delete(Long id) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		GameTable tableToDelete = (GameTable) session.createCriteria(GameTable.class)
			.add(Restrictions.eq("id", id)).uniqueResult();
		session.delete(tableToDelete);
		session.getTransaction().commit();
	}

	@Override
	public List<GameTable> getGameTables() {
		Session session = HibernateUtil.getSession();
		List<GameTable> players = (List<GameTable>) session.createCriteria(GameTable.class).list();
		return players;
	}

}
